﻿
function initArea(hasDefTxt, hId, pId) {
    var MyArea = this;
    this.selWidth = "100";
    this.areaSeries = 1;//1\2\3级联动
    this.selStyle = "margin-right:3px;";
    this.hasDefTxt = hasDefTxt; //是否有默认值
    this.defPtxt = ""; //省市区默认选项
    this.defCtxt = "";
    this.defZtxt = "";
    this.provId = pId; //省
    this.cityId = null; //市
    this.zoneId = null; //县区
    this.hidAreaId = hId; //保存选中ID
    this.isSetHid = false; //下拉选择时是否保存选中ID
    this.isGanged = true; //选择省份时是否同时显示城市和县区
    this.showSelect = 3; //默认显示哪些控件（1：只显示省，2：显示省、市，3：显示省、市、区）

    if (this.hasDefTxt) {
        this.defPtxt = "— 省份 —";
        this.defCtxt = "— 城市 —";
        this.defZtxt = "— 县区 —";
    }
    var argv = initArea.arguments;
    this.cityId = (argv.length > 3) ? argv[3] : null;
    this.zoneId = (argv.length > 4) ? argv[4] : null;
    if (this.cityId != null) {
        this.areaSeries = 2;
        if (this.zoneId != null)
            this.areaSeries = 3;
    }

    if (this.selWidth != "")
        this.selStyle += "width:" + this.selWidth + "px";

    this.init = function () {
        document.write("<input type=hidden id=" + this.hidAreaId + " name=" + this.hidAreaId + "><select id=" + this.provId + " name=" + this.provId + " style='" + this.selStyle + "'></select>");
        this.CreateOption(this.provId, "0", 1, this.defPtxt); //初始化省份
        if (this.areaSeries != 1)
            $("#" + this.provId).change(function () { MyArea.CreateOption(MyArea.cityId, this.value, 2, MyArea.defCtxt); });
        else
            $("#" + this.provId).change(function () { MyArea.setHidVal(this.value, 1) });
        if (this.areaSeries > 1 && this.cityId != null) {
            var index = this.hasDefTxt ? 1 : 0;
            this.CreateOption(this.cityId, $("#" + this.provId).children().eq(index).val(), 2, this.defCtxt);
            if (this.areaSeries > 2 && this.zoneId != null && this.isGanged)
                this.CreateOption(this.zoneId, $("#" + this.cityId).children().eq(index).val(), 3, this.defZtxt);
        }
    },
    this.setArea = function () {
        var argv = this.setArea.arguments;
        if (argv.length > 0) {
            var areaId = argv[0];
            //设置省市区默认选中值
            $.ajax({
                type: "get",
                datatype: "text",
                url: "/WebService/Area.asmx/GetRegionsString",
                data: { id: areaId },
                ifModified: true,
                async: false, //true->异步(并)[默认]；false->同步(串)
                success: function (result) {
                    var data = result.text;
                    var provVal = data.split(',').length > 0 ? data.split(',')[0] : null;
                    var cityVal = data.split(',').length > 1 ? data.split(',')[1] : null;
                    var zoneVal = data.split(',').length > 2 ? data.split(',')[2] : null; 
                    if (provVal != null && provVal != "") {
                        $("#" + MyArea.provId).val(provVal);
                        if (cityVal != null && cityVal != "") {
                            MyArea.CreateOption(MyArea.cityId, provVal, 2, MyArea.defCtxt, cityVal);
                            if (MyArea.zoneId != null) {
                                if (zoneVal != null && zoneVal != "")
                                    MyArea.CreateOption(MyArea.zoneId, cityVal, 3, MyArea.defZtxt, zoneVal);
                                else
                                    MyArea.CreateOption(MyArea.zoneId, cityVal, 3, MyArea.defZtxt);
                            }
                        }
                    }
                }
            });
        }
    },
    this.createSelect = function (objId, grade) {
        //如果没有下拉框则添加
        if ($("#" + objId).length == 0) {
            var select = document.createElement("select");
            select.id = objId;
            select.name = objId;
            select.style.cssText = MyArea.selStyle;
            if (MyArea.areaSeries == 3 && grade == 2)
                $(select).change(function () { MyArea.CreateOption(MyArea.zoneId, this.value, 3, MyArea.defZtxt); });
            else
                $(select).change(function () { MyArea.setHidVal(this.value, grade); });
            $(select).appendTo($("#" + MyArea.provId).parent());
        }
    },
    this.CreateOption = function (objId, id, grade, defTxt) {
        MyArea.createSelect(objId, grade);
        var temp = "";
        $("#" + objId).empty(); //清空Select
        if (defTxt != "")
            $("<option></option>").val("").text(defTxt).appendTo($("#" + objId));
        if (id != "") {
            var argv = MyArea.CreateOption.arguments;
            var defValue = (argv.length > 4) ? argv[4] : null;
            $.ajax({
                type: "post",
                url: "/WebService/Area.asmx/GetRegionsList",
                data: "{ Pid: '" + id + "'}",
                contentType: "application/json",
                dataType: 'json',
                ifModified: true,
                async: false,
                success: function (result) {
                    if (result.d != "") {
                        var jsonlist = eval(result.d);
                        if (jsonlist.length > 0) {
                            $("#" + objId).show();

                            for (i = 0; i < jsonlist.length; i++)
                                $("<option></option>").val(jsonlist[i].id).text(jsonlist[i].name).appendTo($("#" + objId));
                            
                            var pid = jsonlist[0].id;
                            if (defValue != null) {
                                $("#" + objId).val(defValue);
                                temp = defValue;
                                pid = defValue;
                            }
                            else {
                                //下拉选择时是否保存下拉框选中ID
                                if (MyArea.isSetHid) {
                                    if (MyArea.hasDefTxt)
                                        temp = id;
                                    else
                                        temp = pid;
                                }
                                //选择省份时是否同时显示城市和县区
                                if (MyArea.areaSeries == 3 && grade == 2 && MyArea.isGanged)
                                    MyArea.CreateOption(MyArea.zoneId, pid, 3, MyArea.defZtxt);
                            }
                        }
                        else {
                            $("#" + objId).hide();
                            temp = id;
                            if (MyArea.areaSeries == 3 && grade == 2 && MyArea.isGanged)
                                $("#" + MyArea.zoneId).hide();
                        }
                    }
                }
            });
        }
        else {
            $("#" + objId).hide();
            if (MyArea.areaSeries == 3 && grade == 2 && MyArea.isGanged)
                $("#" + MyArea.zoneId).hide();
        }
        MyArea.setHidVal(temp, grade);
    },
    this.setHidVal = function (id, grade) {
        //保存选中值
        if (id == "" && MyArea.isSetHid) {
            if (grade == 3)
                id = $("#" + MyArea.cityId).val();
            else if (grade == 2)
                id = $("#" + MyArea.provId).val();
        }
        $("#" + MyArea.hidAreaId).val(id);
    },
    this.hasSelect = function () {
        return $("#" + MyArea.hidAreaId).val() != "" ? true : false;
    }
    this.init();
}